package com.thinkcube.edutainment;

import java.io.File;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class EduApplication extends Application {

	private Thread.UncaughtExceptionHandler androidDefaultUEH;
	private Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {

		public void uncaughtException(Thread thread, Throwable ex) {

			Log.e("EduApplication", "Uncaught exception is: ", ex);

			// log it & phone home.

			androidDefaultUEH.uncaughtException(thread, ex);

		}

	};

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		androidDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(handler);
		initImageLoader(getApplicationContext());

	}

	public static void initImageLoader(final Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.

		File cacheDir = StorageUtils.getCacheDirectory(context);
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.threadPoolSize(1)
				.memoryCache(new WeakMemoryCache())
				.memoryCacheSize(100 * 1024 * 1024)
				.memoryCacheSizePercentage(13)

				// default
				.discCache(new UnlimitedDiscCache(cacheDir))
				// default
				.discCacheSize(200 * 1024 * 1024).discCacheFileCount(100)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);

	}
}
